package com.yq.spbYq.controller;

import com.yq.spbYq.User;
import com.yq.spbYq.service.UserService;
import org.springframework.beans.factory
        .annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/api")
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestParam String username, @RequestParam String password, @RequestParam String captcha) {
        User user = userService.validateUser(username, password, captcha);
        Map<String, Object> response = new HashMap<>();
        if (user != null) {
            response.put("success", true);
        } else {
            response.put("success", false);
            response.put("message", "用户名、密码或验证码错误！");
        }
        return response;
    }
    @PostMapping("/insertUser")
    public Map<String, Object> insertUser(@RequestBody User user) {
        int result = userService.insertUser(user);
        Map<String, Object> response = new HashMap<>();
        if (result > 0) {
            response.put("success", true);
        } else {
            response.put("success", false);
            response.put("message", "注册失败！");
        }
        return response;
    }
    @DeleteMapping("/deleteUserByUsername")
    public Map<String, Object> deleteUserByUsername(@RequestParam String username) {
        int result = userService.deleteUserByUsername(username);
        Map<String, Object> response = new HashMap<>();
        if (result > 0) {
            response.put("success", true);
        } else {
            response.put("success", false);
            response.put("message", "删除失败！");
        }
        return response;
    }
    @PutMapping("/updateUser")
    public Map<String, Object> updateUser(@RequestBody User user) {
        int result = userService.updateUser(user);
        Map<String, Object> response = new HashMap<>();
        if (result > 0) {
            response.put("success", true);
        } else {
            response.put("success", false);
            response.put("message", "更新失败！");
        }
        return response;
    }
    @GetMapping("/selectAllUsers")
    public Map<String, Object> selectAllUsers() {
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("data", userService.selectAllUsers());
        return response;
    }
}